﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VietImage.Models;
using VietImage.Supports;
using VietImage.Roles;
using System.IO;

namespace VietImageSoftware
{
	/// <summary>
	/// Interaction logic for ManageImageForm.xaml
	/// </summary>
	public partial class ManageImageForm : Window
    {
        private ReadXMLHelper xmlHelper;

        internal ReadXMLHelper XmlHelper
        {
            get { return xmlHelper; }
            set { xmlHelper = value; }
        }

        private Boolean isEdited;

        private int prior = -1;

        public int Prior
        {
            get { return prior; }
            set { prior = value; }
        }

        private Week currentWeek;

        public Week CurrentWeek
        {
            get { return currentWeek; }
            set { currentWeek = value; }
        }

        private DateTime currentDate;

        public DateTime CurrentDate
        {
            get { return currentDate; }
            set { currentDate = value; }
        }

        private int currentMonth;

        public int CurrentMonth
        {
            get { return currentMonth; }
            set { currentMonth = value; }
        }

        private int currentYear;

        public int CurrentYear
        {
            get { return currentYear; }
            set { currentYear = value; }
        }

        private RegisteredUserRole userRole;

        public RegisteredUserRole UserRole
        {
            get { return userRole; }
            set { userRole = value; }
        }

        private VietImage.Models.Image currentImage;

        public VietImage.Models.Image CurrentImage
        {
            get { return currentImage; }
            set { currentImage = value; }
        }

        public String SubCategoryName { get; set; }

		public ManageImageForm()
		{
			this.InitializeComponent();
			// Insert code required on object creation below this point.
		}


        #region Manage Image
        private List<VietImage.Models.DetailImage> lDetailImages;


        private void cbMICountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((Country)cbMICountry.SelectedItem).ID != currentImage.City.ID_Country)
                isEdited = true;
        }

        private void cbMILanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbMILanguage.SelectedIndex >= 0 && gridManageImage.IsVisible)
            {
                int lang = cbMILanguage.SelectedIndex;

                txtMIDescription.Text = lDetailImages[lang].Description;
                txtMIName.Text = lDetailImages[lang].Name;
                txtMITag.Text = lDetailImages[lang].Tag;

                Category ic = currentImage.Category;

                txtMISubCategory.Text = ic.GetDetail(((VietImage.Models.Language)cbMILanguage.SelectedItem).ID).Name;
            }
        }

        private void btnMIManage_Click(object sender, RoutedEventArgs e)
        {
            Manage();
            isEdited = false;
        }

        private void btnMICancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Manage()
        {
            currentImage.Author = txtMIAuthor.Text;
            xmlHelper.ImageAuthor = txtMIAuthor.Text;
            currentImage.TakenTime = dpMITakenTime.SelectedDate.Value;
            City city = new City();
            city.Name = txtMICity.Text;
            city.ID_Country = int.Parse(cbMICountry.SelectedValue.ToString());
            List<ErrorItem> errorList = City.Insert(ref city, 1);
            if (errorList.Count > 0)
            {
                MessageForm frm = new MessageForm();
                frm.ErrorList = errorList;
                frm.ShowDialog();
                return;
            }
            txtMICity.Text = city.Name;
            currentImage.ID_City = city.ID;
            MessageForm form = new MessageForm();
            form.CurrentImage = currentImage;
            form.DetailImages = lDetailImages;
            form.UserRole = userRole;
            form.HaveOKButton = true;
            form.TxtOK = "Yes";
            form.TxtCancel = "No";
            form.TxtTitle = "Warning";
            form.TxtMessage = "Do you want to change this image?";
            form.ShowDialog();
            LoadImage();
        }

        private void txtMIName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (cbMILanguage.SelectedIndex >= 0)
            {
                if (lDetailImages[cbMILanguage.SelectedIndex].Name != txtMIName.Text)
                    isEdited = true;
                lDetailImages[cbMILanguage.SelectedIndex].Name = txtMIName.Text;
            }
        }

        private void txtMIDescription_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (cbMILanguage.SelectedIndex >= 0)
            {
                if (lDetailImages[cbMILanguage.SelectedIndex].Description != txtMIDescription.Text)
                    isEdited = true;
                lDetailImages[cbMILanguage.SelectedIndex].Description = txtMIDescription.Text;
            }
        }

        private void txtMITag_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (cbMILanguage.SelectedIndex >= 0)
            {
                if (lDetailImages[cbMILanguage.SelectedIndex].Tag != txtMITag.Text)
                    isEdited = true;
                lDetailImages[cbMILanguage.SelectedIndex].Tag = txtMITag.Text;
            }
        }

        private void btnMIFinish_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Manage();
            Close();
        }

        private void btnMINextImage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (isEdited)
                Manage();
            VietImage.Models.Image img = null;
            if (prior == -1)
                img = currentImage.GetNextSameStatusImage();
            else if (prior == 0)
                img = currentImage.GetNextSameStatusImage(currentDate);
            else if (prior == 1)
                img = currentImage.GetNextSameStatusImage(currentWeek.BeginDate, currentWeek.EndDate);
            else if (prior == 2)
                img = currentImage.GetNextSameStatusImage(currentMonth, currentYear);
            if (img == null)
            {
                MessageForm form = new MessageForm();
                form.HaveOKButton = false;
                form.TxtTitle = "Warning";
                form.TxtCancel = "OK";
                form.TxtMessage = "No next image";
                form.ShowDialog();
            }
            else
            {
                currentImage = img;
                LoadImage();
            }
        }

        private void btnMIPrevImage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (isEdited)
                Manage();
            VietImage.Models.Image img = null;
            if (prior == -1)
                img = currentImage.GetPreviousSameStatusImage();
            else if (prior == 0)
                img = currentImage.GetPreviousSameStatusImage(currentDate);
            else if (prior == 1)
                img = currentImage.GetPreviousSameStatusImage(currentWeek.BeginDate, currentWeek.EndDate);
            else if (prior == 2)
                img = currentImage.GetPreviousSameStatusImage(currentWeek.BeginDate, currentWeek.EndDate);
            if (img == null)
            {
                MessageForm form = new MessageForm();
                form.HaveOKButton = false;
                form.TxtTitle = "Warning";
                form.TxtCancel = "OK";
                form.TxtMessage = "No previous image";
                form.ShowDialog();
            }
            else
            {
                currentImage = img;
                LoadImage();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            xmlHelper = new ReadXMLHelper();
            LoadImage();
            cbMILanguage.Items.Clear();
            int index = 0;
            foreach (Language l in VietImage.Models.Language.GetList())
            {
                if (l.ID != userRole.ID_Language)
                    index++;
                cbMILanguage.Items.Add(l);
            }
            cbMILanguage.SelectedValuePath = "ID";
            cbMILanguage.DisplayMemberPath = "Language1";
            cbMILanguage.SelectedIndex = index;
            txtMISubCategory.Text = SubCategoryName;
        }

        private void LoadImage()
        {
            if (currentImage != null)
            {
                lDetailImages = currentImage.GetDetails();

                txtMIAuthor.Text = currentImage.Author;
                dpMITakenTime.SelectedDate = currentImage.TakenTime;

                if (cbMICountry.Items.Count == 0)
                {
                    cbMICountry.Items.Clear();
                    foreach (Country c in Country.GetList())
                        cbMICountry.Items.Add(c);
                    cbMICountry.SelectedValuePath = "ID";
                    cbMICountry.DisplayMemberPath = "Name";
                }

                Category ic = currentImage.Category;

                txtMISubCategory.Text = ic.GetDetail(userRole.ID_Language).Name;

                City city = currentImage.City;
                cbMICountry.SelectedValue = city.ID_Country;
                txtMICity.Text = city.Name;
                int index = 0;
                cbMILanguage.SelectedIndex = -1;
                foreach (Language l in cbMILanguage.Items)
                    if (l.ID != userRole.ID_Language)
                        index++;
                cbMILanguage.SelectedIndex = index;
                using (var stream = new MemoryStream(currentImage.Source.ToArray()))
                {
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.StreamSource = stream;
                    bitmap.EndInit();
                    bitmap.Freeze();
                    imgMIImage.Source = bitmap;
                }
            }
            isEdited = false;
        }
        #endregion

        private void txtMIAuthor_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtMIAuthor.Text != currentImage.Author)
                isEdited = true;
        }

        private void txtMICity_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtMICity.Text != currentImage.City.Name)
                isEdited = true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            xmlHelper.WriteXML();
        }

        private void gridManageImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch (Exception ex)
            {
            }
        }
	}
}